'use strict';

const LOANLODASH = Symbol('Application#LoanLoadash');
const FS = Symbol('Application#Fs');
const MOMENT = Symbol('Application#Moment');
const ApiClient = require('../../lib/index').ApiClient;
// const DingTalkClient = require('../../lib/index').DingTalkClient;

const APICLIENT = Symbol('Application#apiClient');
// const DINGTALKClIENt = Symbol('Application#dingTalkClient');

module.exports = {

  get lodash() {
    if (!this[LOANLODASH]) {
      this[LOANLODASH] = require('lodash');
    }
    return this[LOANLODASH];
  },

  apiClient(appkey,appsecret,url) {
    if (!this[APICLIENT]) {
      this[APICLIENT] = new ApiClient({
        'appkey':appkey,
        'appsecret': appsecret,
        'REST_URL': url
    });
    }
    return this[APICLIENT];
  },

  // dingTalkClient(appkey,appsecret,url) {
  //   if (!this[DINGTALKClIENt]) {
  //     this[DINGTALKClIENt] = new DingTalkClient({
  //       'appkey':appkey,
  //       'appsecret': appsecret,
  //       'url': url
  //   });
  //   }
  //   return this[DINGTALKClIENt];
  // },



  get moment() {
    if (!this[MOMENT]) {
      this[MOMENT] = require('moment');
    }
    return this[MOMENT];
  },

  get fs() {
    if (!this[FS]) {
      this[FS] = require('fs');
    }
    return this[FS];
  },


  format_time(times) {
    if (times) {
      return this.moment(times).format('YYYY-MM-DD HH:mm:ss');
    }
    return this.moment().format('YYYY-MM-DD HH:mm:ss');
  },

  /**
   *
   * @param {时间戳} times
   */
  getMicDateDuration(times) {
    let startDate;
    let endDate;
    if (times) {

       endDate = this.moment(times * 1000);
       startDate = this.moment(times * 1000).subtract(15,'m'); //加15分钟

    }else {
       endDate = this.moment();
       startDate = this.moment().subtract(15,'m'); //加15分钟
    }
     return {
        startDate: startDate.unix(),
        endDate: endDate.unix()
     };
  },

  // 获取信用等级
  getCreditRate(rateNum){
    const map = {
      0:"默认",
      100:"A",
      101:"B",
      102:"C"
      ,103:"D",
      104:"E",
      105:"F"
    };
  return this.lodash.get(map, rateNum, 'A'); // 匹配不同等级
  },

  current_format_time(times) {
    if (times) {
      return this.moment(times).format('HH:mm:ss');
    }
    return this.moment().format('HH:mm:ss');

  },

  async_write_file(filepath,sql){
    this.fs.writeFile(filepath,sql,{flag:'a',encoding:'utf-8',mode:'0666'},function(err){
      if(err){
          console.log("文件写入失败")
      }else{
          console.log("文件写入成功");
      }
     });
  },

  // 删除文件
  async_del_file(filepath){
    fs.unlink(filepath,function(error){
        if(error){
            console.log(error);
            return false;
        }
        console.log('删除文件成功');
    });

  },

  format_date(times) {
    if (times) {
      return this.moment(times).format('YYYY-MM-DD');
    }
    return this.moment().format('YYYY-MM-DD');
  },

  /**
   * 是否是生产环境
   *
   */
  get isProduction() {
    return this.config.env === 'prod';
  },

  /**
   * 是否是预发布环境
   *
   */
  get isPreview() {
    return this.config.env === 'preview';
  },

  /**
   * 是否是集成测试环境
   *
   */
  get isSit() {
    return this.config.env === 'sit';
  },

  /**
   * 是否是在测试中
   *
   */
  get isTesting() {
    return this.config.env === 'unittest';
  },

};
